﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Web;
using System.Web.Caching;
using TPOJ.BLL;
using TPOJ.CacheDependencyFactory;
using TPOJ.Model;

namespace TPOJ.Web
{
    public static class ContestDataProxy
    {
        private static readonly int contestTimeout = int.Parse(ConfigurationManager.AppSettings["ContestCacheDuration"]);
        private static readonly bool enableCaching = bool.Parse(ConfigurationManager.AppSettings["EnableCaching"]);

        public static IList<Contest> GetContests()
        {
            ContestBO contestBO = new ContestBO();

            if (!enableCaching)
                return contestBO.GetContests();

            string key = "contest";

            IList<Contest> data = (IList<Contest>)HttpRuntime.Cache[key];

            if (data == null)
            {
                data = contestBO.GetContests();

                AggregateCacheDependency aggrCacheDependency = DependencyFacade.GetContestDependency();

                HttpRuntime.Cache.Add(key, data, aggrCacheDependency, DateTime.Now.AddHours(contestTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return data;
        }

        public static Contest GetContest(int id)
        {
            ContestBO contestBO = new ContestBO();

            if (!enableCaching)
                return contestBO.GetContest(id);

            string key = "contest" + id;
            Contest data = (Contest)HttpRuntime.Cache[key];

            if (data == null)
            {
                data = contestBO.GetContest(id);

                AggregateCacheDependency aggrCacheDependency = DependencyFacade.GetContestDependency();

                HttpRuntime.Cache.Add(key, data, aggrCacheDependency, DateTime.Now.AddHours(contestTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return data;
        }

        internal static IList<Standing> GetContestStanding(int id)
        {
            ContestBO contestBO = new ContestBO();

            if (!enableCaching)
                return contestBO.GetContestStanding(id);

            string key = "contest_standing" + id;
            IList<Standing> data = (IList<Standing>)HttpRuntime.Cache[key];

            if (data == null)
            {
                data = contestBO.GetContestStanding(id);

                AggregateCacheDependency aggrCacheDependency = DependencyFacade.GetStandingDependency();

                HttpRuntime.Cache.Add(key, data, aggrCacheDependency, DateTime.Now.AddHours(contestTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return data;
        }

        public static IList<Contest> GetCurrentContests()
        {
            ContestBO contestBO = new ContestBO();

            if (!enableCaching)
                return contestBO.GetCurrentContests();

            string key = "currentContest";
            IList<Contest> data = (IList<Contest>)HttpRuntime.Cache[key];

            if (data == null)
            {
                data = contestBO.GetCurrentContests();

                AggregateCacheDependency aggrCacheDependency = DependencyFacade.GetContestDependency();

                HttpRuntime.Cache.Add(key, data, aggrCacheDependency, DateTime.Now.AddHours(contestTimeout), Cache.NoSlidingExpiration, CacheItemPriority.High, null);
            }
            return data;
        }
    }
}